module.exports = function($scope, $resource, $modal, $modalInstance, getDate, sale, updateFlag, toaster){

	var url;
	$scope.updateFlag = updateFlag;
	$scope.groupobj = {
		'pay_mode': 'credit',
		'sale_code': sale.sale_code
	};
	$scope.date = {
		'lable': getDate(new Date()),
		'opened': false
	}

	$scope.getBalance = function(){
		$scope.supplier_balance = 0;
        $resource('/api/as/pc/bmaccount/getBalanceByCodeList', {}, {}).save({'company_code': sale.company_code}, function (res) {
            if (res.errcode === 0) {
                res.data.forEach(element => {
                    if(sale.company_code == element.supplier_company_code){
                        $scope.supplier_balance = element.balance_price * 0.01;
                    }
                });
            }
        });
	}
	
	if(updateFlag){
		url = '/api/as/tc/grouporder/update';
		$scope.groupobj.code = sale.code;

		$scope.groupobj.pay_mode = sale.pay_mode;
		$scope.groupobj.group_code = sale.group_code;
		$scope.groupobj.plan_count = sale.plan_count;
		$scope.groupobj.arrival_date = sale.arrival_date;
		$scope.groupobj.guide_name = sale.guide_name;
		$scope.groupobj.guide_mobile = sale.guide_mobile;
		$scope.groupobj.vehicle_number = sale.vehicle_number;
		$scope.groupobj.remark = sale.remark;
	} else {
		url = '/api/ac/tc/groupOrderService/createOrder';
		$scope.getBalance();
		$scope.groupCodePr = $scope.date.lable.replace(/-/g, '');
	}




	$scope.dateOpen = function ($event, item) {
		$event.preventDefault();
		$event.stopPropagation();
		item.opened = true;
	};

    //保存
	$scope.gogo = function(info){
		if(!$scope.groupobj.pay_mode){
			toaster.warning({title: '', body: '支付类型不能为空'});
			return;
		}
		if(!$scope.date.lable){
			toaster.warning({title: '', body: '出游时间不能为空'});
			return;
		} else {
			$scope.groupobj.arrival_date = getDate($scope.date.lable);
		}
		if(!updateFlag){
			if(!$scope.groupobj.group_codesuf){
				toaster.warning({title: '', body: '游团编号不能为空'});
				return;
			}
			$scope.groupobj.group_code = info.company_code + $scope.groupCodePr + $scope.groupobj.group_codesuf;
		}
		
		if(!$scope.groupobj.guide_name){
			toaster.warning({title: '', body: '导游姓名不能为空'});
			return;
		}
		if(!$scope.groupobj.guide_mobile){
			toaster.warning({title: '', body: '导游电话不能为空'});
			return;
		}
		if(!$scope.groupobj.vehicle_number){
			toaster.warning({title: '', body: '车牌号不能为空'});
			return;
		}
		
		$resource(url, {}, {}).save($scope.groupobj, function(res) {
			if (res.errcode === 0) {
				toaster.success({title: '', body: '成功'});
				$modalInstance.close();
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	};

	$scope.close = function(){
		$modalInstance.dismiss('cancel');
	}

};
